package com.JackTang.servlets;

import com.JackTang.fruit.dao.FruitDAO;
import com.JackTang.fruit.dao.impl.FruitDAOImpl;
import com.JackTang.fruit.pojo.Fruit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // POST 方式下，设置编码，防止中文乱码
        // GET 方式目前不需要设置编码(基于Tomcat8)
        // 需要注意的是， 设置编码这一句代码必须在所有的获取参数动作之前
        request.setCharacterEncoding("UTF-8");
        // 请求是被封装在 req 这个对象中
        String fname = request.getParameter("fname"); // 获取 fname
        Integer price = Integer.parseInt(request.getParameter("price"));// 通过http 请求发过来的只能是字符串
        Integer fcount = Integer.parseInt(request.getParameter("fcount"));
        String remark = request.getParameter("remark");

        System.out.println("fname = " + fname);
        System.out.println("price = " + price);
        System.out.println("fcount = " + fcount);
        System.out.println("remark = " + remark);


        FruitDAO fruitDAO = new FruitDAOImpl();
        boolean flag = fruitDAO.addFruit(new Fruit(0,fname, price, fcount, remark));

        System.out.println(flag ? "添加成功！" : "添加失败！");

    }
}